package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.LoginUser;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/*
* 实现自己数据库查询判断
* 返回UserDetails的实现类
*
* */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        Map<String, Object> map = new HashMap<>();
        map.put("account",s);
        Map<String, Object> user = userMapper.getUserInfo(map);
        if(user==null){
            throw new RuntimeException("用户账号或者密码错误");
        }
        Map<String,Object> params= new HashMap<>();
        params.put("account",s);
        //用户接口权限信息
        List<Map<String,Object>> list = userMapper.getInterId(params);
        //移除null
        list.removeAll(Collections.singleton(null));
        List<String> interId=new ArrayList<>();
        if(list!=null&&list.size()>0){
            interId= list.stream()
                    .map(it -> it.get("interId").toString())
                    .collect(Collectors.toList());
        }
        //查询权限，存入LoginUser 权限信息时Collection类型
        return new LoginUser(user,interId);
    }
}
